﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
using Unity.Transforms;
using Unity.Mathematics;
using Unity.Jobs;
using Unity.Burst;

[AlwaysSynchronizeSystem]
public class AreaOverlapSystem : ComponentSystem
{
    //private const bool NATIVEDEBUG = false;
    //EntityQuery mGroup;

    protected override void OnCreate()
    {
        base.OnCreate();
    }

    public List<uint> OverlapFilt(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4)
    {
        List<uint> ans = new List<uint>();
        Entities.WithAll<VoxelComponentData>().ForEach((Unity.Entities.Entity entity, ref Translation trans) =>
        {
            Vector2 p = new Vector2(trans.Value.x, trans.Value.y);
            if (Vector2.Dot(p2 - p1, p - p1)>=0 && Vector2.Dot(p4 - p1, p - p1)>=0 && Vector2.Dot(p4 - p3, p - p3)>=0 && Vector2.Dot(p2 - p3, p - p3)>=0)
            {
                ans.Add((uint)entity.Index);
            }
        });
        return ans;
    }
    /* struct AreaOverlapJob : IJobChunk   
     {
         public ArchetypeChunkComponentType<Translation> translationType;

         public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
         {
             var chunkTranslation = chunk.GetNativeArray(translationType);
             for (var i = 0; i < chunk.Count; i++)
             {
                 var trans = chunkTranslation[i];
                 if(MainGame.waitForOverlapCheck.isPointInArea(trans.Value.x, trans.Value.y))
                 {
                     MainGame.overlapList.Add(TerrianAPI.ID.IDEncode(new Vector3(trans.Value.x, trans.Value.y, 0)));
                 }
             }

         }
     }*/

    protected override void OnUpdate()
    {
    }
}
